package com.jingdianjichi.subject.domain.service.impl;

import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.enums.ISDeletedFlagEnum;
import com.jingdianjichi.subject.domain.convert.SubjectInfoBoConverter;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBo;
import com.jingdianjichi.subject.domain.handler.subject.SubjectTypeHandler;
import com.jingdianjichi.subject.domain.handler.subject.SubjectTypeHandlerFactory;
import com.jingdianjichi.subject.domain.service.SubjectInfoDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectInfoService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {
    @Resource
    private SubjectInfoService subjectInfoService;
    @Resource
    private SubjectTypeHandlerFactory subjectTypeHandlerFactory;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectLabelService subjectLabelService;

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        if(log.isInfoEnabled()){
            log.info("SubjectInfoDomainServiceImpl----add:{}",subjectInfoBo);
        }
        //一个工厂包含四种类型，根据传入的type自动映射选择器
        SubjectInfo subjectInfo = SubjectInfoBoConverter.INSTANCE.convertBoToInfo(subjectInfoBo);
        subjectInfo.setIsDeleted(ISDeletedFlagEnum.UN_DELETE.getCode());
        subjectInfoService.insert(subjectInfo);
        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());
        //获取主键id
        subjectInfoBo.setId(subjectInfo.getId());
        handler.add(subjectInfoBo);
        //关联表插入
        List<SubjectMapping> subjectMappingList=new LinkedList<>();

        List<Integer> categoryIds = subjectInfoBo.getCategoryIds();
        List<Integer> labelIds = subjectInfoBo.getLabelIds();
        categoryIds.forEach(categoryId->{
            labelIds.forEach(labelId->{
                SubjectMapping subjectMapping=new SubjectMapping();
                subjectMapping.setSubjectId(subjectInfo.getId());
                subjectMapping.setCategoryId(Long.valueOf(categoryId));
                subjectMapping.setLabelId(Long.valueOf(labelId));
                subjectMapping.setIsDeleted(ISDeletedFlagEnum.UN_DELETE.getCode());
                subjectMappingList.add(subjectMapping);
            });
        });
        subjectMappingService.insertBatch(subjectMappingList);
    }

    /**
     * 分页查询
     * @param subjectInfoBo
     * @return
     */
    @Override
    public PageResult<SubjectInfoBo> getSubjectPage(SubjectInfoBo subjectInfoBo) {

        PageResult<SubjectInfoBo> pageResult=new PageResult<>();
        pageResult.setPageNo(subjectInfoBo.getPageNo());
        pageResult.setPageSize(subjectInfoBo.getPageSize());
        Integer start = (subjectInfoBo.getPageNo() - 1) * subjectInfoBo.getPageSize();

        SubjectInfo subjectInfo = SubjectInfoBoConverter.INSTANCE.convertBoToInfo(subjectInfoBo);
        Integer count=subjectInfoService.countByCondition(subjectInfo,subjectInfoBo.getCategoryId()
                ,subjectInfoBo.getLabelId());
        if(count==0){
            return null;
        }
        List<SubjectInfo> subjectInfoList=subjectInfoService.queryPage(subjectInfo,subjectInfoBo.getCategoryId()
                ,subjectInfoBo.getLabelId(),start,subjectInfoBo.getPageSize());
        List<SubjectInfoBo> subjectInfoBoList = SubjectInfoBoConverter.INSTANCE.convertInfoListToBoList(subjectInfoList);
        pageResult.setRecords(subjectInfoBoList);
        pageResult.setTotal(count);
        return pageResult;
    }

    /**
     * 查询题目详情
     * @param subjectInfoBo
     * @return
     */
    @Override
    public SubjectInfoBo querySubjectInfo(SubjectInfoBo subjectInfoBo) {

        SubjectInfo subjectInfo = subjectInfoService.queryById(subjectInfoBo.getId());

        SubjectTypeHandler handler = subjectTypeHandlerFactory.getHandler(subjectInfo.getSubjectType());

        SubjectOptionBo optionBO = handler.query(subjectInfo.getId().intValue());

        SubjectInfoBo bo = SubjectInfoBoConverter.INSTANCE.convertOptionAndInfoToBo(optionBO, subjectInfo);

        SubjectMapping subjectMapping = new SubjectMapping();

        subjectMapping.setSubjectId(subjectInfo.getId());

        subjectMapping.setIsDeleted(ISDeletedFlagEnum.UN_DELETE.getCode());

        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);

        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());

        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);

        List<String> labelNameList = labelList.stream().map(SubjectLabel::getLabelName).collect(Collectors.toList());

        bo.setLabelName(labelNameList);
        return bo;

    }
}
